package com.fiberhome.ztelauncher.core;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.fiberhome.ztelauncher.xml.bean.ElementAction;
import com.fiberhome.ztelauncher.xml.bean.ExtraData;

public class ActionUtil {
	 /**
     * 页面跳转
     *
     * @param context
     * @param elementAction
     * @param extraData
     */
    public static void goTo(Context context, ElementAction elementAction, ExtraData extraData) {
        Intent intent = new Intent();
        if ("1".equals(elementAction.actionType)) {        //1.跳转到video
            intent.setClass(context, VideoActivity.class);
            intent.putExtra("jumpURL", elementAction.actionURL);
            context.startActivity(intent);
        }  else if ("3".equals(elementAction.actionType)) {        //3.系统浏览器网页(打开指定页面)
        	try {
        		intent.setAction(Intent.ACTION_VIEW);
                Uri uri = Uri.parse(elementAction.actionURL);
                intent.setData(uri);
                context.startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }else if ("4".equals(elementAction.actionType)) {   //4.跳转指定的应用
        	try {
        		if (elementAction != null && elementAction.params.params != null) {
                    for (int j = 0; j < elementAction.params.params.size(); j++) {
                        intent.setAction(elementAction.params.params.get(j).value);
                    }
                }
        		Bundle bundle = new Bundle();
        		bundle.putString("jumpURL", elementAction.actionURL);
//                intent.putExtra("jumpURL", elementAction.actionURL);
                if (extraData != null && extraData.extraData != null) {
                    for (int j = 0; j < extraData.extraData.size(); j++) {
                    	bundle.putString(extraData.extraData.get(j).name, extraData.extraData.get(j).value);
                    }
                }
                Log.i("TAG", "action = " + intent.getAction());
//                context.startActivity(intent);
                intent.putExtras(bundle);
                context.sendBroadcast(intent);
			} catch (Exception e) {
				e.printStackTrace();
			}
            
        } 
    }
}
